export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

//处理树
export function mapTree (org) {
    const haveChildren = Array.isArray(org.children) && org.children.length > 0;
    if(org.path && org.name){
	    return {
	    		id: org.id,
	        orderNum: org.orderNum,
	        name: org.path,
	        path: org.name,
	        meta: org.meta,
	        component: org.path,
	        //判断它是否存在子集，若果存在就进行再次进行遍历操作，知道不存在子集便对其他的元素进行操作
	        children: haveChildren ? org.children.map(i => mapTree(i)) : []
	    };
    }
}


export function findTreeId(array, id) {
  let stack = [];
  let going = true;
  let walker = (array, id) => {
    array.forEach(item => {
      if (!going) return;
      stack.push(item['id']);
      if (item['id'] === id) {
        going = false;
      } else if (item['children']) {
        walker(item['children'], id);
      } else {
        stack.pop();
      }
    });
    if (going) stack.pop();
  }
  walker(array, id);
  if (stack.length && stack) {
    return stack;
  } else {
    return false
  }

}

export function filPath(str) {
  if (str) {
    if (str.includes('_')) {
      return str.substring(0, str.indexOf('_'))
    } else {
      return str
    }
  }
}

//转路径
export function tranformStr(str) {
  var strArr = str.split('');
  for (var i = 1; i < strArr.length; i++) {
    if (strArr[i] == '-' || strArr[i] == '_') {
      //删除'-'
      strArr.splice(i, 1);
      //转大写
      if (i < strArr.length) {
        strArr[i] = strArr[i].toUpperCase();
      }
    }
  }
  return strArr.join('');
}

//路由的
export function recursionData(menuTree) {
  for (let i = 0; i < menuTree.length; i++) {
		if (menuTree[i].path) {
				menuTree[i].component = 'Layout'
		    menuTree[i].path = '/' + menuTree[i].path
		}else{
			menuTree.splice(i,1)
			i--
		}
	}
  return menuTree
}
